// pages/auth/auth.js

import { request } from "../../request/index.js"
import { login } from "../../utils/asyncWx.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({
  // clickGetUserInfo(e){
    //   const { encryptedData,rawData,iv,signature } = e.detail;
    //   wx.login({
      //     timeout:10000,
      //     success: (result)=>{
        //       const {code} = result;
        //     }
        //   });
        // }
  // 获取用户信息
  async clickGetUserInfo(e){
    // try {} catch()出错处理
    try {
      const { encryptedData,rawData,iv,signature } = e.detail;
    // 获取小程序登录成功的code
    const {code} = await login();
    const loginParams={ encryptedData,rawData,iv,signature,code }
    // 发送请求 获取用户的token
    const {token} = await request({url:"/users/wxlogin",data:loginParams,methods:"POST"});
    // console.log(res);
    // 把token 存入缓存中 同时跳转回上一个页面
    wx.setStorageSync("token", token);
    wx.navigateBack({
      delta: 1
    });
    } catch (error) {
      console.log(error);
    }
  }
})